/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.basedata.category.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BaseCategoryMapper;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BaseCategoryExample;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.site.sx.mag.basedata.category.enums.CategoryType;

/**
 * 基础分类Service
 * @author 北冥
 * @version 2018-12-28
 */
@Service
@Transactional(readOnly = true)
public class BaseBizCategoryService{

	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public List<BaseCategory> queryCategory(CategoryType categoryType) {
		BaseCategoryExample baseCategoryExample = new BaseCategoryExample();
		BaseCategoryExample.Criteria criteria = baseCategoryExample.createCriteria();
		
		criteria.andCategoryTypeEqualTo(categoryType.value);
		
		List<BaseCategory> dlist = baseCategoryMapper.selectByExample(baseCategoryExample);
		setParentCategory(dlist);
		return dlist;
	}
	/**
	 * 
	 * @param dlist 分类list
	 * @param dmap 存储类ID 和 学校
	 * @return
	 */
	public List<BaseCategory> removeNullCategory(List<BaseCategory> dlist,Map<String,List<BizCompany>> dmap){
		Map<String,BaseCategory> cmap = new HashMap<String,BaseCategory>();
		List<BaseCategory> dlistEnd = new ArrayList<BaseCategory>();
		for(BaseCategory baseCategory : dlist) {
			if(StringUtils.isNotBlank(baseCategory.getParentId())) {
				//二级分类
				if(dmap.get(baseCategory.getId()).size()>0) {
					dlistEnd.add(baseCategory);
					cmap.put(baseCategory.getParentId(), baseCategory);
				}
			}
		}
		
		
		for(BaseCategory baseCategory : dlist) {
			if(StringUtils.isBlank(baseCategory.getParentId())) {
				//一级分类
				if(cmap.get(baseCategory.getId())!=null) {
					dlistEnd.add(baseCategory);
				}else if(dmap.get(baseCategory.getId()).size()>0) {
					dlistEnd.add(baseCategory);
				}
			}
		}	
		return dlistEnd;
	}
	/*
	 * 树装结构查询，主要给mag界面做树用
	 */
	@Cacheable(value = "category", key = "#root.targetClass+#root.methodName" + "+#root.args")
	public BaseCategory queryById(String categoryId) {
		BaseCategory baseCategory = baseCategoryMapper.selectByPrimaryKey(categoryId);
		return baseCategory;
	}
	
	public void clearParentCategory(BaseCategory baseCategory) {
		if(baseCategory.getParentCategory()==null || StringUtils.isBlank(baseCategory.getParentCategory().getId())) {
			baseCategory.setParentId("");	
		}
	}
	
	public void setParentCategory(BaseCategory baseCategory) {
		if(baseCategory!=null && StringUtils.isNotEmpty(baseCategory.getParentId())) {
			
			BaseCategory baseParentCategory = baseCategoryMapper.selectByPrimaryKey(baseCategory.getParentId());
			baseCategory.setParentCategory(baseParentCategory);
			
		}
		if(baseCategory!=null && baseCategory.getParentCategory()!=null && StringUtils.isNotEmpty(baseCategory.getParentCategory().getId())) {
			baseCategory.setParentId(baseCategory.getParentCategory().getId());
		}
	}
	
	
	public void setParentCategory(List<BaseCategory> baseCategoryList) {
		for(BaseCategory baseCategory : baseCategoryList) {
			setParentCategory(baseCategory);
		}
	}
}